package com.ostrich.library_common.utils

import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.ostrich.library_common.R

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/28 11:12
 * @Description : ImageAdapter
 */
class ImageAdapter {
    companion object{
        /**
         * 为布局设置网络背景图片
         */
        @JvmStatic
        @BindingAdapter("android:setBgImage")
        fun setBgImage(viewGroup: ViewGroup,url:String?){
            Glide.with(viewGroup)
                .asBitmap()
                .load(url)
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?,
                    ) {
                        val drawable = BitmapDrawable(resource)
                        viewGroup.background = drawable
                    }

                })

        }




        @JvmStatic
        @BindingAdapter("android:setRoundedImage")
        fun setRoundedImage( imageView:ImageView, url:String?){

            Glide.with(imageView)
                .load(url)
                .placeholder(R.drawable.common_tools_iv_bg)
                .error(R.drawable.common_tools_iv_bg)
                .transform(CenterCrop(),RoundedCorners(15))
                .into(imageView)
        }

        @JvmStatic
        @BindingAdapter("android:setImage")
        fun setImage( imageView:ImageView, url:String?){
            Glide.with(imageView)
                .load(url)
                .placeholder(R.drawable.common_tools_iv_bg)
                .error(R.drawable.common_tools_iv_bg)
                .transform(CenterCrop())
                .into(imageView)
        }
        @JvmStatic
        @BindingAdapter("android:setCircleImage")
        fun setCircleImage( imageView:ImageView, url:String?){
            Glide.with(imageView)
                .load(url)
                .placeholder(R.drawable.common_tools_iv_bg)
                .error(R.drawable.common_tools_iv_bg)
                .transform(CircleCrop())
                .into(imageView)
        }
    }
}